package org.springblade.core.tool.datamask;

import org.springblade.core.tool.utils.Func;
import org.springframework.util.StringUtils;

public enum DataMaskingFunc {
	/**
	 *  脱敏转换器
	 */
	NO_MASK((str, maskChar, maskRange) -> {
		return str;
	}),
	RANGE_MASK((str, maskChar, maskRange) -> {
		if (StringUtils.hasLength(str)) {
			int start = 0,end = 0;
			if(!Func.isEmpty(maskRange) && maskRange.contains(",")){
				start = Integer.parseInt(maskRange.split(",")[0]);
				end = Integer.parseInt(maskRange.split(",")[1]);
			}else{
				start = Integer.parseInt(maskRange);
				end = str.length();
			}
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < str.length(); i++) {
				if(i >= start && i <= end){
					sb.append(StringUtils.hasLength(maskChar) ? maskChar : DataMaskingOperation.MASK_CHAR);
				}else{
					sb.append(str.substring(i,i+1));
				}
			}
			return sb.toString();
		} else {
			return str;
		}
	}),
	ALL_MASK((str, maskChar, maskRange) -> {
		if (StringUtils.hasLength(str)) {
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < str.length(); i++) {
				sb.append(StringUtils.hasLength(maskChar) ? maskChar : DataMaskingOperation.MASK_CHAR);
			}
			return sb.toString();
		} else {
			return str;
		}
	});

	private final DataMaskingOperation operation;

	private DataMaskingFunc(DataMaskingOperation operation) {
		this.operation = operation;
	}

	public DataMaskingOperation operation() {
		return this.operation;
	}
}
